package com.example.nna.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.nna.contain.containPage1;
import com.example.nna.main.R;
import com.example.nna.quickaction.QuickAction;
import com.example.nna.quickaction.QuickAction.OnActionItemClickListener;
import com.example.nna.voz.Voz;

public class listViewCustom1 extends BaseAdapter{
     private ArrayList<containPage1> contains;
     private LayoutInflater inflater; 
     private ViewHolder holder;  
     private float mTextSize1;
     private float mTextSize2;
     private float mTextSize3;
     private float mTextSize4;

	 public listViewCustom1(Activity context,ArrayList<containPage1> contains) {  
		 //super();         
		 this.contains = contains;
	   //  this.mHandler = handler;
	     this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
	 }  
	public void setSize(float mTextSize){
		TextView t1 = (TextView)inflater.inflate(R.layout.list_item, null).findViewById(R.id.text1);
		TextView t2 = (TextView)inflater.inflate(R.layout.list_item, null).findViewById(R.id.text2);
		TextView t3 = (TextView)inflater.inflate(R.layout.list_item, null).findViewById(R.id.text3);
		TextView t4 = (TextView)inflater.inflate(R.layout.list_item, null).findViewById(R.id.text4);
		this.mTextSize1 = t1.getTextSize()*mTextSize;
		this.mTextSize2 = t2.getTextSize()*mTextSize;
		this.mTextSize3 = t3.getTextSize()*mTextSize;
		this.mTextSize4 = t4.getTextSize()*mTextSize;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return contains.size(); 
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	public static class ViewHolder  
	{  
        ImageView imgView;  
        TextView txtView1;  
        TextView txtView2;
        TextView txtView3;  
        TextView txtView4;  
        RelativeLayout layout;
	}  
	private ColorStateList newColorStateList( int colorDefault, int colorPressed){
		return new ColorStateList(
    			new int[][]{new int [] {android.R.attr.state_pressed},new int [0]},
    			new int []{colorPressed,colorDefault});
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int pos;
		pos = position;
        if(convertView==null)  
        {  
            holder = new ViewHolder();  
            convertView = inflater.inflate(R.layout.list_item, null);  
            holder.layout = (RelativeLayout) convertView.findViewById(R.id.listitem1);  
            holder.imgView = (ImageView) convertView.findViewById(R.id.image);  
            holder.txtView1 = (TextView) convertView.findViewById(R.id.text1);  
            holder.txtView2 = (TextView) convertView.findViewById(R.id.text2);  
            holder.txtView3 = (TextView) convertView.findViewById(R.id.text3);
            holder.txtView4 = (TextView) convertView.findViewById(R.id.text4);

            convertView.setTag(holder);  
        }  
        else  
            holder=(ViewHolder)convertView.getTag();  
        
        if((contains.get(position).Image()==null)&&(contains.get(position).Posts()==null)){
        	holder.txtView1.setTextColor(newColorStateList(Color.WHITE, Color.BLUE));
        	holder.layout.setBackgroundResource(R.color.vozTcat);
        	holder.txtView1.setText(contains.get(position).Forum()); 
        	holder.txtView2.setText("");  
            holder.txtView3.setText("");  
            holder.txtView4.setText("");
        	holder.txtView2.setVisibility(View.GONE); 
            holder.txtView3.setVisibility(View.GONE); 
            holder.txtView4.setVisibility(View.GONE); 
        }else{
        	holder.txtView2.setVisibility(View.VISIBLE); 
            holder.txtView3.setVisibility(View.VISIBLE); 
            holder.txtView4.setVisibility(View.VISIBLE); 
            holder.layout.setBackgroundResource(R.color.vozThread);
        	holder.txtView1.setTextColor(newColorStateList(Color.BLACK, Color.BLUE));
        	holder.txtView2.setTextColor(newColorStateList(Color.BLACK, Color.BLUE));
        	holder.txtView3.setTextColor(newColorStateList(Color.BLACK, Color.BLUE));
        	holder.txtView4.setTextColor(newColorStateList(Color.BLACK, Color.BLUE));
        	holder.txtView1.setText(contains.get(position).Forum());
            holder.txtView2.setText(contains.get(position).LastPost());  
            holder.txtView3.setText(contains.get(position).Threads());  
            holder.txtView4.setText(contains.get(position).Posts());  
        }
        holder.txtView1.setTextSize(mTextSize1);
        holder.txtView2.setTextSize(mTextSize2);
        holder.txtView3.setTextSize(mTextSize3);
        holder.txtView4.setTextSize(mTextSize4);
        
       // holder.imgView.setImageBitmap(ScaleBitmap(contains.get(position).Image(),mTextSize1));
        holder.imgView.setImageBitmap(contains.get(position).Image());
        
        
        holder.txtView1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//mHandler.obtainMessage(Voz.MSG_PAGE2,pos).sendToTarget();
				if(mItemClickListener != null){
					mItemClickListener.onItemClick(pos, 1);
				}
			}
		});
        holder.txtView2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mItemClickListener != null){
					mItemClickListener.onItemClick(pos, 2);
				}
			}
		});
        holder.txtView1.setFocusable(true);
        holder.txtView1.setClickable(true);
        holder.txtView2.setFocusable(true);
        holder.txtView2.setClickable(true);
        return convertView;  
	}
	private OnActionItemClickListener mItemClickListener;
	public void setOnActionItemClickListener(OnActionItemClickListener listener) {
		mItemClickListener = listener;
	}
	public interface OnActionItemClickListener {
		public abstract void onItemClick( int pos, int actionId);
	}
}
